# form local
from image.ImageCapture import captrue
from image.ImageArea import cArea
import sys
import cv2
import numpy as np
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QLineEdit, QPushButton, QHBoxLayout, QFormLayout
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QTimer


class ImageWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("图像显示")
        self.setGeometry(100, 100, 640, 480)

        # 创建一个QLabel用于显示图片
        self.label = QLabel(self)

        # 创建一个定时器来定期更新图像
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_image)
        self.timer.start(500)  # 每500毫秒更新一次图像，即每秒更新2次

        # 截图区域的坐标变量
        self.x1, self.y1, self.x2, self.y2 = 100, 100, 400, 400  # 默认值
        self.chatArea = cArea(100, 100,400, 400)
        # 创建输入框布局
        self.init_ui()

    def init_ui(self):
        # 创建输入框
        self.x1_input = QLineEdit(self)
        self.y1_input = QLineEdit(self)
        self.x2_input = QLineEdit(self)
        self.y2_input = QLineEdit(self)

        # 设置输入框的默认值
        self.x1_input.setText(str(self.x1))
        self.y1_input.setText(str(self.y1))
        self.x2_input.setText(str(self.x2))
        self.y2_input.setText(str(self.y2))

        # 创建提交按钮
        self.submit_button = QPushButton("提交", self)
        self.submit_button.clicked.connect(self.update_coordinates)

        # 创建布局
        layout = QVBoxLayout()
        layout.addWidget(self.label)

        # 创建水平布局，放置左上角的文本提示和输入框
        point_input_layout1 = QHBoxLayout()
        point_input_layout1.addWidget(self.x1_input)  # 左上角X输入框
        point_input_layout1.addWidget(self.y1_input)  # 左上角Y输入框

        # 创建表单布局，添加左上角X 和 Y 的文本提示
        form_layout1 = QFormLayout()
        form_layout1.addRow("左上角坐标:", point_input_layout1)
        layout.addLayout(form_layout1)

        # 创建水平布局，放置右下角的文本提示和输入框
        point_input_layout2 = QHBoxLayout()
        point_input_layout2.addWidget(self.x2_input)  # 右下角X输入框
        point_input_layout2.addWidget(self.y2_input)  # 右下角Y输入框
        # 创建表单布局，添加右下角X 和 Y 的文本提示
        form_layout2 = QFormLayout()
        form_layout2.addRow("右下角坐标:", point_input_layout2)

        layout.addLayout(form_layout2)

        layout.addWidget(self.submit_button)

        # 设置窗口的布局
        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

    def update_coordinates(self):
        # 从输入框获取坐标并更新变量
        try:
            self.x1 = int(self.x1_input.text())
            self.y1 = int(self.y1_input.text())
            self.x2 = int(self.x2_input.text())
            self.y2 = int(self.y2_input.text())
        except ValueError:
            print("输入无效，请输入整数值")
        self.chatArea = cArea(self.x1, self.y1, self.x2, self.y2)

    def update_image(self):
        # 获取指定区域的截图并更新显示
        roi = captrue(self.chatArea)

        # 将BGR格式图像转换为RGB格式
        frame_rgb = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)

        # 转换为QImage
        h, w, ch = frame_rgb.shape
        bytes_per_line = ch * w
        q_image = QImage(frame_rgb.data, w, h, bytes_per_line, QImage.Format_RGB888)

        # 显示图像
        self.label.setPixmap(QPixmap.fromImage(q_image))

    def closeEvent(self, event):
        # 在关闭应用时释放摄像头资源
        pass  # 由于我们不再使用摄像头，这里没有需要释放的资源


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = ImageWindow()
    window.show()
    sys.exit(app.exec_())
